﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Medical.Consts;
using Sector7.Medical.Utility;
using Sector7.Framework.Enums;
using Sector7.Medical.Biz;
using Sector7.Medical.EntityType;
using Sector7.Framework.Utility;

namespace Sector7.Medical.INFM.Windows
{
    /// <summary>
    /// SetSickBedLevelChargeItemWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SetSickBedLevelChargeItemWindow : AddModeWindowBase
    {
        #region Public Functions
        public decimal CountCost
        {
            get { return m_countCost; }
        }

        public List<M7_ChargeItem> ChargeItem
        {
            get { return m_selectedItemList; }
        }
        #endregion

        #region Private Fucntions
        /// <summary>
        /// 数据加载
        /// </summary>
        private void SetSickBedLevelChargeItemWindow_Loaded(object obj, RoutedEventArgs e)
        {
            Senvironments.ProgressWindow.Visibility = Visibility.Visible;

            var worker = BackgroundWorkerUtil.GetWorker(this);
            worker.DoWork += new System.ComponentModel.DoWorkEventHandler(LoadData_DoWork);
            worker.RunWorkerAsync();
        }

        /// <summary>
        /// 加载数据工作线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadData_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            GetData();

            this.Dispatcher.BeginInvoke(new DelegateWithNothing(LoadData));
        }

        /// <summary>
        /// 获取所有收费项信息列表
        /// </summary>
        private void GetData()
        {
            m_chargeItemList = Agent_INFS.GetChargeItemList();
        }

        private void LoadData()
        {
            if (m_chargeItemList != null)
            {
                foreach (var chargeItem in m_chargeItemList)
                {
                    wrpMain.Children.Add(CreateChargeItemControl(chargeItem));
                }
            }

            Senvironments.ProgressWindow.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// 控件加载方法
        /// </summary>
        /// <param name="chargeItem"></param>
        /// <returns></returns>
        private StackPanel CreateChargeItemControl(M7_ChargeItem chargeItem)
        {
            var stackPanelControl = new StackPanel();
            stackPanelControl.Margin = new Thickness(0, 3, 0, 3);
            stackPanelControl.Orientation = Orientation.Horizontal;
            stackPanelControl.Width = 150;
            stackPanelControl.Height = 30;
            stackPanelControl.Tag = chargeItem;

            var checkBoxControl = new CheckBox();
            checkBoxControl.VerticalAlignment = VerticalAlignment.Center;
            checkBoxControl.Width = 95;
            checkBoxControl.Click +=  new RoutedEventHandler(SelectItemChange);
            checkBoxControl.Content = chargeItem.ChargeItemName;
            
            stackPanelControl.Children.Add(checkBoxControl);

            var labelControl = new Label();
            labelControl.VerticalContentAlignment = VerticalAlignment.Center;
            labelControl.HorizontalContentAlignment = HorizontalAlignment.Right;
            labelControl.Width = 55;
            labelControl.Content = chargeItem.Price;
            labelControl.Tag = chargeItem.ChargeItemId;
            checkBoxControl.Tag = labelControl;
            stackPanelControl.Children.Add(labelControl);

            return stackPanelControl;
        }

        /// <summary>
        /// 计算总费用
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        private void SelectItemChange(object obj, RoutedEventArgs e)
        {
            var ckb = obj as CheckBox;
            bool isChecked = (bool)ckb.IsChecked;
            Label label = ckb.Tag as Label;
            M7_ChargeItem chargeItem = new M7_ChargeItem();

            if (isChecked)
            {
                countAllCost += Convert.ToDecimal(label.Content);
                chargeItem.ChargeItemId = Convert.ToInt32(label.Tag);
                chargeItem.ChargeItemName = ckb.Content.ToString();
                chargeItem.Price = Convert.ToDecimal(label.Content);
                chargeItem.Flags = C_ChargeItemFlags.FIXED_CHARGE;
                m_tempSelectedItemList.Add(chargeItem);
            }
            else
            {
                countAllCost -= Convert.ToDecimal(label.Content);
                chargeItem.ChargeItemId = Convert.ToInt32(label.Tag);
                chargeItem.Price = Convert.ToDecimal(label.Content.ToString());

                for (int i = 0; i < m_tempSelectedItemList.Count; i++)
                {
                    var objItem = m_tempSelectedItemList[i];
                    if (chargeItem.ChargeItemId == objItem.ChargeItemId)
                    {
                        if (m_tempSelectedItemList.Contains(objItem))
                        {
                            m_tempSelectedItemList.Remove(objItem);
                        }
                    }
                }
            }

            lblAmount.Text = countAllCost.ToString();
            m_selectedItemList = m_tempSelectedItemList;
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (CheckData())
            {
                AddData();
            }
            else
            {
                ShowErrors();
            }
        }

        protected override bool  CheckData()
        {
            m_errors.Clear();

            if (m_selectedItemList == null || m_selectedItemList.Count == 0)
            {
                MessageBoxResult mbr = Alert.ShowDialog(AlertType.Question, "当前未选择任何收费项,是否继续？");
                if (mbr == MessageBoxResult.Yes)
                {
                    this.DialogResult = true;
                }
                return false;
            }
            else
            {
                try
                {
                    Convert.ToDecimal(lblAmount.Text.Trim());
                }
                catch
                {
                    m_errors.Add("总费用", "只能是数字");
                }

                return base.CheckData();
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        protected override void AddData()
        {
            m_countCost = Convert.ToDecimal(lblAmount.Text.Trim());

            if (m_tempSelectedItemList != null)
            {
                m_selectedItemList = m_tempSelectedItemList;
            }
            base.AddData();
            this.DialogResult = true;
        }

        #endregion

        #region Structure
        public SetSickBedLevelChargeItemWindow()
        {
            InitializeComponent();
        }
        #endregion

        #region Inner Members
        /// <summary>
        /// 所计算的费用
        /// </summary>
        private decimal m_countCost;

        /// <summary>
        /// 计算出所有费用
        /// </summary>
        private decimal countAllCost = 0;

        /// <summary>
        /// 所有收费项列表
        /// </summary>
        private List<M7_ChargeItem> m_chargeItemList;

        /// <summary>
        /// 所有选中收费项列表
        /// </summary>
        private List<M7_ChargeItem> m_selectedItemList;

        /// <summary>
        /// 所有选中项的临时值
        /// </summary>
        private List<M7_ChargeItem> m_tempSelectedItemList = new List<M7_ChargeItem>();
        #endregion
    }
}
